package gui.jfram;

import bll.service.StudentService;
import bll.serviceimpl.StudentServiceImpl;
import dal.entity.Student;
import gui.datamodel.SimpleTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainFrame extends JFrame {
    private JLabel jLabel1;
    private JTable jTable1;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenu menuEdit;
private JMenu menuAbout;
private JMenuItem itemExcel;
private JMenuItem itemExit;
private JMenuItem itemAdd;
private JMenuItem itemUpdate;
private JMenuItem itemAbout;
    //定义列的名称
    private StudentService studentService;
    private SimpleTableModel simpleTableModel;
    private List<String> cols;
    private List<Student>rows;
    private List<String> colsName;

    public MainFrame() {
        jLabel1 = new JLabel();
        jTable1 = new JTable(56, 6);
        jLabel1.setIcon(new ImageIcon("src//gui//img//backimg.jpg"));
        menuBar = new JMenuBar();
        menuFile = new JMenu("文件");
        menuAbout = new JMenu("帮助");
        menuEdit = new JMenu("编辑");
        itemExcel = new JMenuItem("导出Excel");
        itemExit = new JMenuItem("退出");
        menuFile.add(itemExcel);
        menuFile.add(itemExit);
        itemAdd = new JMenuItem("添加学生");
        itemUpdate= new JMenuItem("编辑学生");
        menuEdit.add(itemAdd);
        menuEdit.add(itemUpdate);
        itemAbout = new JMenuItem("关于");
        menuAbout.add(itemAbout);

        this.setJMenuBar(menuBar);
        menuBar.add(menuFile);
        menuBar.add(menuEdit);
        menuBar.add(menuAbout);


        this.add(jLabel1, BorderLayout.NORTH);
        this.add(jTable1, BorderLayout.CENTER);

        //列的初始化
        studentService = new StudentServiceImpl();
        cols = new ArrayList<>();
        cols.add("姓名");
        cols.add("学号");
        cols.add("年龄");
        cols.add("联系方式");
        cols.add("家庭住址");
        rows = studentService.getAllStudent();
        colsName =new ArrayList<>();
        colsName.add("sname");
        colsName.add("snumber");
        colsName.add("sage");
        colsName.add("sphone");
        colsName.add("saddress");
        //
        simpleTableModel = new SimpleTableModel(cols,rows,colsName);
        jTable1.setModel(simpleTableModel);

        this.setTitle("JTable");
        this.setSize(1160, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        //导出Excel
        itemExcel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                fileChooser.showDialog(MainFrame.this,"导出Excel");//显示文件对话框
                File file = fileChooser.getSelectedFile();
                String fileName = file.getAbsolutePath() +"\\student.xls";
                studentService.toExcel(rows,fileName);
            }
        });

        //退出
        itemExit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               System.exit(0);
            }
        });
        itemAbout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MainFrame.this,"软件管理系统 \n 2022级软件工程本科3班 苏子豪");
            }
        });
    }

}
